package cn.wolfcode.web.controller;

import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.AccountLog;
import cn.wolfcode.domain.OperateIntergralVo;
import cn.wolfcode.service.IUsableIntegralService;
import cn.wolfcode.web.msg.IntergralCodeMsg;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 积分服务
 */
@RestController
@RequestMapping("/intergral")
public class IntegralController {
    @Autowired
    private IUsableIntegralService usableIntegralService;
    //减积分接口
    @RequestMapping("/decrIntergral")
    Result<String> decrIntergral(@RequestBody OperateIntergralVo operateIntergralVo){
        try {
            usableIntegralService.decrIntergral(operateIntergralVo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(IntergralCodeMsg.INTERGRAL_NOT_ENOUGH);
        }
        return Result.success("当前用户减积分操作成功");
    }
    //加积分
    @Transactional
    @RequestMapping("/incrIntergral")
    Result<Boolean> incrIntergral(@RequestBody OperateIntergralVo operateIntergralVo){
        try {
//            usableIntegralService.incrInrergral(operateIntergralVo);
            //分布式事务：调用分支事务中的try()方法
            BusinessActionContext businessActionContext = new BusinessActionContext();//可以手动创建上下文
            usableIntegralService.initTry(businessActionContext,operateIntergralVo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(IntergralCodeMsg.OP_INTERGRAL_ERROR);
        }
        return Result.success(true);//加积分成功
    }

}
